/**
 * 
 */
package com.tien.utils;

/**
 * @author tien
 * 
 */
public class PagingBean {
	private Integer totalRows;
	private Integer totalPages;
	private Integer currentPage;
	private Integer rowsPerPage;

	private Integer startDisplayPage = 1;
	private Integer endDisplayPage = 10;
	private Integer allDisplayPagings = 10;

	private Integer startRow;
	private Integer endRow;

	public PagingBean(Integer totalRows, Integer totalPages,
			Integer rowsPerPage, Integer currentPage, Integer allDisplayPagings) {
		super();
		this.allDisplayPagings = allDisplayPagings;
		this.totalRows = totalRows;
		this.currentPage = totalRows == 0 ? 0 : currentPage;
		this.rowsPerPage = rowsPerPage;
		this.totalPages = totalPages;

		startDisplayPage = (currentPage - allDisplayPagings / 2) > 0 ? currentPage
				- allDisplayPagings / 2
				: 1;

		endDisplayPage = startDisplayPage + allDisplayPagings <= totalPages ? startDisplayPage
				+ allDisplayPagings - 1
				: totalPages;
		setStartRow(startRow);
		setEndRow(endRow);
	}

	public PagingBean(Integer totalRows, Integer totalPages,
			Integer rowsPerPage, Integer currentPage) {
		this(totalRows, totalPages, rowsPerPage, currentPage, 10);
	}

	public Integer getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(Integer totalRows) {
		this.totalRows = totalRows;
	}

	public Integer getStartRow() {
		return startRow;
	}

	public void setStartRow(Integer startRow) {
		int start = this.currentPage * rowsPerPage - rowsPerPage;
		if (null != startRow && startRow > start) {
			this.startRow = startRow;
		} else {
			this.startRow = this.totalRows == 0 ? 0 : this.currentPage
					* rowsPerPage - rowsPerPage;
		}
	}

	public Integer getEndRow() {
		return endRow;
	}

	public void setEndRow(Integer endRow) {
		this.endRow = totalPages >= 1 ? (this.currentPage * rowsPerPage > this.totalRows ? this.totalRows
				: this.currentPage * rowsPerPage)
				: this.totalRows;
	}

	public Integer getStartDisplayPage() {
		return startDisplayPage;
	}

	public void setStartDisplayPage(Integer startDisplayPage) {
		this.startDisplayPage = startDisplayPage;
	}

	public Integer getEndDisplayPage() {
		return endDisplayPage;
	}

	public void setEndDisplayPage(Integer endDisplayPage) {
		this.endDisplayPage = endDisplayPage;
	}

	public Integer getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(Integer totalPages) {
		this.totalPages = totalPages;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getRowsPerPage() {
		return rowsPerPage;
	}

	public void setRowsPerPage(Integer rowsPerPage) {
		this.rowsPerPage = rowsPerPage;
	}

	public Integer getAllDisplayPagings() {
		return allDisplayPagings;
	}

	public void setAllDisplayPagings(Integer allDisplayPagings) {
		this.allDisplayPagings = allDisplayPagings;
	}

	@Override
	public String toString() {
		return "totalRows: " + totalRows + " totalPages: " + totalPages
				+ " currentPage: " + currentPage + " rowsPerPage: "
				+ rowsPerPage + " startRow: " + startRow + " endRow: " + endRow
				+ " startDisplayPage: " + this.startDisplayPage
				+ " endDisplayPage: " + this.endDisplayPage;
	}
}
